<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="选择店铺" prop="goodsShopName">
                 
        <el-select
          v-model="queryParams.goodsShopName"
          placeholder="请选择店铺"
        >
          <el-option v-for="(item,index) in teaShops" :key="index" :label="item.shopName" :value="item.shopId" />        
        </el-select>
               
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="defaultPrice">
        <el-input
          v-model="queryParams.defaultPrice"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否在卖" prop="isSell">
        <el-select
          v-model="queryParams.isSell"
          placeholder="请选择是否在卖"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['milktea:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['milktea:goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['milktea:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['milktea:goods:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="teaShop.shopName" />
      <el-table-column label="栏目名称" align="center" prop="shopCommodity.tradeName" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="显示次序" align="center" prop="displayOrder" />
      <el-table-column label="价格" align="center" prop="defaultPrice" />
      <el-table-column label="是否在卖" align="center" prop="isSell">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.isSell"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品图片" align="center" prop="image">
        <template slot-scope="scope">
          <img :src="scope.row.image" style="width:60px;height:40px" />
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['milktea:goods:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['milktea:goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择店铺" prop="goodsShopName">
          <el-select v-model="form.goodsShopName" placeholder="请选择店铺">
            <el-option v-for="(item,index) in teaShops" :key="index" :label="item.shopName" :value="item.shopId" />        
          </el-select>
        </el-form-item>
        <el-form-item label="选择栏目" prop="goodsCategoryName">
          <el-select v-model="form.goodsCategoryName" placeholder="请选择栏目">
            <el-option v-for="(item,index) in columnList" :key="index" :label="item.tradeName" :value="item.id" />        
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="显示次序" prop="displayOrder">
          <el-input v-model="form.displayOrder" placeholder="请输入显示次序" />
        </el-form-item>
        <el-form-item label="价格" prop="defaultPrice">
          <el-input v-model="form.defaultPrice" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="是否在卖">
          <el-radio-group v-model="form.isSell">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品图片">
          <imageUpload v-model="form.image" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  teaShops,
  shopCommodity,
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
} from "@/api/milktea/goods";

export default {
  name: "Goods",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 店铺查询数据
      teaShops:[],
      // 栏目数据
      columnList:[],
      // 修改店铺默认值
      shopName:null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsCategoryName: null,
        goodsShopName: null,
        name: null,
        defaultPrice: null,
        isSell: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsShopName: [
          { required: true, message: "店铺名称不能为空", trigger: "change" },
        ],
        goodsCategoryName: [
          { required: true, message: "栏目名称不能为空", trigger: "change" },
        ],
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        displayOrder: [
          { required: true, message: "显示次序不能为空", trigger: "blur" },
        ],
        defaultPrice: [
          { required: true, message: "价格不能为空", trigger: "blur" },
        ],
        isSell: [
          { required: true, message: "是否在卖不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log("goodsList:",response.rows);
      });
      teaShops().then((res)=>{
        this.teaShops = res.rows;
        console.log("teaShops:",res.rows);
      })
      shopCommodity().then((res)=>{
        this.columnList = res.rows;
        console.log("columnList:",res.rows);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsCategoryName: null,
        goodsShopName: null,
        name: null,
        displayOrder: null,
        defaultPrice: null,
        isSell: 0,
        image: null,
        description: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        console.log(response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改商品列表";
        this.form.goodsShopName = response.data.teaShop.shopId;
        this.form.goodsCategoryName = response.data.shopCommodity.id;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品列表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "milktea/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
